package com.yunhui.admin.utils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

/**
 * 分页sevice
 * @author Administrator
 *
 */
@Service
public class PaginationService {

	public Log logger = LogFactory.getLog(PaginationService.class);

	private int pageSizeDef = 50;

	private Map<String, Object> searchMap;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public long getTotalRecord(Object obj, String executeMethodName) {
		Class clazz = obj.getClass();
		searchMap.remove("pageStart");
		searchMap.remove("pageSize");
		try {
			Method method = clazz.getMethod(executeMethodName, Map.class);
			Object result = method.invoke(obj, new Object[] { searchMap });
			return result == null ? 0 : (Long) result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取统计条数失败", e);
		}
		return 0;
	}

	public void encapSearchPaginationCondition(HttpServletRequest request, Map<String, Object> paramMap) {
		String pageIndex = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		searchMap = new HashMap<String, Object>();
		searchMap.putAll(paramMap);
		paramMap.put("pageStart", this.getPageStart(pageIndex, pageSize));
		int rows = pageSizeDef;
		try {
			rows = Integer.parseInt(request.getParameter("rows") == null ? "0" : request.getParameter("rows"));
		} catch (Exception e) {
		}
		paramMap.put("pageSize", rows);
		// System.out.println("searchMap=" + searchMap);
	}

	private int getPageStart(String pageIndex, String pageSize) {
		int pageStart = 0;
		if (pageIndex != null && pageSize != null && !pageIndex.equals("0") && !pageSize.equals("NaN")) {
			int pz = pageSizeDef;
			try {
				pz = Integer.valueOf(pageSize);
			} catch (Exception e) {
			}
			pageStart = (Integer.valueOf(pageIndex) - 1) * pz;
		}
		return pageStart;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public long getTotalRecord(Object obj, String executeMethodName, Map<String, Object> paramMap) {
		Class clazz = obj.getClass();
		try {
			Method method = clazz.getMethod(executeMethodName, Map.class);
			Object result = method.invoke(obj, new Object[] { paramMap });
			return result == null ? 0 : (Long) result;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取统计条数失败", e);
		}
		return 0;
	}

	public Map<String, Object> getPaginationMap(HttpServletRequest request) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String pageIndex = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		resultMap.put("pageStart", this.getPageStart(pageIndex, pageSize));
		resultMap.put("pageSize", Integer.parseInt(request.getParameter("rows") == null ? "0" : request.getParameter("rows")));
		return resultMap;
	}

	public void removePage() {
		if (searchMap != null) {
			searchMap.remove("pageStart");
			searchMap.remove("pageSize");
		}
	}
}
